//
//  EditAfterAddGuestViewController.m
//  QRMaster
//
//  Created by myApple on 15/4/29.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "EditAfterAddGuestViewController.h"
#import "GuestModel.h"
#import "PromptInfoView.h"
#import "addGuestInfoView.h"
#import "RequestNetWork.h"
#import "RoomSeclectViewController.h"
#import "Reachability.h"
#import "GroupGuestLogic.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import "PromptInfoView.h"
#import "RequestNetWork.h"
#import "GuestModel.h"
#import "GuestManagerViewController.h"
#import "validateInputValue.h"
#import "Reachability.h"
#import "MBProgressHUD.h"
#import "GuestLogic.h"
#import "ChineseString.h"
#import "pinyin.h"
#import "AreaChooseController.h"

@interface EditAfterAddGuestViewController ()<addGuestInfoViewDelegate,ABPeoplePickerNavigationControllerDelegate,UITextFieldDelegate>
{
    UIView * editingView;
    UITextField * telePhoneTextField;
    UITextField * nameTextField;
    UITextField * ceritificateTextField;
    UIButton * manBtn;
    UIButton * womanBtn;
    UIImageView * manView;
    UIImageView * womanView;
    NSString * sexFlag;         // 1为男，0为女
    UIButton * roomBtn;
    NSMutableDictionary * saveDict;
    
    UIImageView * editImgView;
    
    UIButton * mobileBtn;
    UIButton * emailBtn;
    UIButton * addAddressTelePhoneBtn;
    UIButton * mobileHeaderBtn;
    UILabel * emailHeaderLabel;
    UILabel * areaCodeIdLabel;
    UILabel * areaLabel;
    
    UIActivityIndicatorView * _activityIndicator;

    UIButton *editBtn;
}

@end

@implementation EditAfterAddGuestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self PrepareGuestView];
    sexFlag =@"1";
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    editBtn.hidden=NO;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.navigationItem.title = NSLocalizedString(@"addGuest", nil);
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    editBtn.hidden = YES;
}
-(void)PrepareGuestView
{
    [self prepareEditingView];
    [self prepareHeadView];
}




//开门权限
-(void)addGuestInfoViewSend
{
    RoomSeclectViewController * rsvc = [[RoomSeclectViewController alloc]init];
    rsvc.communityid = _communityId;
    rsvc.memberid = _model.guestid;
    rsvc.guestOrStaff = @"0";
    [self.navigationController pushViewController:rsvc animated:YES];
}

-(void)addGuestInfoViewBackClick
{
    [self BackClick];
}


-(void)BackClick
{
//    [self dismissViewControllerAnimated:YES completion:^
//     {
//         //编辑或者添加客户要刷新客户列表
//         [[NSNotificationCenter defaultCenter]postNotificationName:@"refreshGuest" object:nil];
//     }];
    [self.navigationController popToRootViewControllerAnimated:YES];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)prepareHeadView
{
    
    editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    editBtn.frame =CGRectMake(UI_SCREEN_WIDTH-50, 10, 20, 20);
    editBtn.backgroundColor=[UIColor clearColor];
    [editBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
    [editBtn setBackgroundImage:[UIImage imageNamed:@"ic_save"] forState:0];
    [self.navigationController.navigationBar addSubview:editBtn];
}

-(void)prepareEditingView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 10;
    float width = ScreenWidth;
    float height = ScreenHeight - y;
    
    editingView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editingView.backgroundColor = RGBA(237, 238, 241, 1);
    [self.view addSubview:editingView];
    
    //类型
    x = 10;
    y = 5;
    width = ScreenWidth - x - 5;
    height = 50;
    UIView * typeView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    typeView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:typeView];
    
    UILabel * typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    typeLabel.text = NSLocalizedString(@"type", nil);
    typeLabel.font = [UIFont systemFontOfSize:14.0];
    [typeView addSubview:typeLabel];
    
    // mobile
    x = 90;
    y = 0;
    width = 40;
    height = 40;
    UIImageView * mobileView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [typeView addSubview:mobileView];
    mobileView.userInteractionEnabled = YES;
    UITapGestureRecognizer * mobileTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(mobileClick)];
    [mobileView addGestureRecognizer:mobileTap];
    
    x = 10;
    y = 20;
    width = 20;
    height = 20;
    mobileBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [mobileBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [mobileBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [mobileBtn addTarget:self action:@selector(mobileClick) forControlEvents:UIControlEventTouchUpInside];
    mobileBtn.selected = YES;
    [mobileView addSubview:mobileBtn];
    
    x = 120;
    y = 20;
    width = 60;
    height = 20;
    UILabel * mobileLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    mobileLabel.text =  NSLocalizedString(@"loginPromt", nil);
    mobileLabel.font = [UIFont systemFontOfSize:14.0];
    [typeView addSubview:mobileLabel];
    
    //email
    x = 180;
    y = 0;
    width = 40;
    height = 40;
    UIImageView * emailImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [typeView addSubview:emailImageView];
    emailImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer * emailTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(emailClick)];
    [emailImageView addGestureRecognizer:emailTap];
    
    x = 10;
    y = 20;
    width = 20;
    height = 20;
    emailBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [emailBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [emailBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [emailBtn addTarget:self action:@selector(emailClick) forControlEvents:UIControlEventTouchUpInside];
    [emailImageView addSubview:emailBtn];
    
    x = 220;
    height = 20;
    y = 20;
    UILabel * emailLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 60, height)];
    emailLabel.text =  NSLocalizedString(@"mineEmail", nil);
    emailLabel.font = [UIFont systemFontOfSize:14.0];
    [typeView addSubview:emailLabel];
    
    //手机号码
    x = 10;
    y = typeView.frame.origin.y + typeView.frame.size.height + 5;
    width = ScreenWidth - x - 5;
    height = 50;
    UIView * telePhoneView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    telePhoneView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:telePhoneView];
    
    x = 100;
    y = 10;
    width = telePhoneView.frame.size.width - x ;
    height = 30;
    telePhoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    telePhoneTextField.font = [UIFont systemFontOfSize:14.0];
    telePhoneTextField.borderStyle = UITextBorderStyleNone;
    telePhoneTextField.placeholder = NSLocalizedString(@"telePhoneNum", nil);
    [telePhoneTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    [telePhoneTextField becomeFirstResponder];
    telePhoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    [telePhoneView addSubview:telePhoneTextField];
    
    //前面点击的区号按钮
    x = 10;
    y = 5;
    width = 40;
    mobileHeaderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    mobileHeaderBtn.frame = CGRectMake(x, y, width, height);
    [mobileHeaderBtn addTarget:self action:@selector(chooseArea) forControlEvents:UIControlEventTouchUpInside];
    [telePhoneView addSubview:mobileHeaderBtn];
    
    areaCodeIdLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 60, 15)];
    areaCodeIdLabel.text = [NSString stringWithFormat:@"+%@",[QRSingleton sharedQRSingleton].currentAreaCodeString];
    areaCodeIdLabel.font = [UIFont systemFontOfSize:10.0];
    [mobileHeaderBtn addSubview:areaCodeIdLabel];
    
    areaLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, 60, 15)];
    areaLabel.font = [UIFont systemFontOfSize:10.0];
    areaLabel.text = [QRSingleton sharedQRSingleton].currentAreaString;
    [mobileHeaderBtn addSubview:areaLabel];
    
    emailHeaderLabel = [[UILabel alloc]initWithFrame:CGRectMake(mobileHeaderBtn.frame.origin.x, mobileHeaderBtn.frame.origin.y + 5, mobileHeaderBtn.frame.size.width, mobileHeaderBtn.frame.size.height)];
    emailHeaderLabel.text = NSLocalizedString(@"mineEmail", nil);
    emailHeaderLabel.hidden = YES;
    emailHeaderLabel.font = [UIFont systemFontOfSize:14.0];
    [telePhoneView addSubview:emailHeaderLabel];
    
    x = telePhoneView.frame.size.width - 50;
    y = 0;
    width = 50;
    height = 50;
    addAddressTelePhoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addAddressTelePhoneBtn.frame = CGRectMake(x, y, width, height);
    [addAddressTelePhoneBtn setBackgroundColor:RGBA(135, 199, 66, 1)];
    [addAddressTelePhoneBtn setBackgroundImage:nil forState:0];
    [addAddressTelePhoneBtn addTarget:self action:@selector(addTelePhone) forControlEvents:UIControlEventTouchUpInside];
    [telePhoneView addSubview:addAddressTelePhoneBtn];
    
    UIImageView * adderssImgView = [[UIImageView alloc]initWithFrame:CGRectMake(10,10, 30, 30)];
    adderssImgView.image = [UIImage imageNamed:@"ic_txl"];
    [addAddressTelePhoneBtn addSubview:adderssImgView];
    
    //姓名
    x = 10;
    y = telePhoneView.frame.size.height + telePhoneView.frame.origin.y + 5;
    width = ScreenWidth - x - 5;
    height = 50;
    UIView * nameView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    nameView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:nameView];
    
    x = 10;
    y = 15;
    width = 40;
    height = 20;
    UILabel * editUserNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editUserNameLabel.text =  NSLocalizedString(@"userName", nil);
    editUserNameLabel.font = [UIFont systemFontOfSize:14.0f];
    [nameView addSubview:editUserNameLabel];
    
    x = 100;
    y = 10;
    width = nameView.frame.size.width - x ;
    height = 30;
    nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    nameTextField.font = [UIFont systemFontOfSize:14.0];
    nameTextField.borderStyle = UITextBorderStyleNone;
    [nameView addSubview:nameTextField];
    
    //身份证
    x = 10;
    y = nameView.frame.size.height + nameView.frame.origin.y + 5;
    width = ScreenWidth - x - 5;
    height = 50;
    UIView * certificateView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    certificateView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:certificateView];
    
    x = 10;
    y = 15;
    width = 60;
    height = 20;
    UILabel * certificateTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    certificateTextLabel.text = NSLocalizedString(@"certificate", nil);
    certificateTextLabel.font = [UIFont systemFontOfSize:14.0f];
    [certificateView addSubview:certificateTextLabel];
    
    x = 100;
    y = 10;
    width = nameView.frame.size.width - x ;
    height = 30;
    ceritificateTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    ceritificateTextField.font = [UIFont systemFontOfSize:14.0];
    ceritificateTextField.borderStyle = UITextBorderStyleNone;
    [certificateView addSubview:ceritificateTextField];
    
    //性别
    x = 10;
    y = 5 + certificateView.frame.size.height + certificateView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = 50;
    UIView * sexView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sexView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:sexView];
    
    
    x = 10;
    y = 15;
    width = 60;
    height = 20;
    UILabel * editSexLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editSexLabel.text = NSLocalizedString(@"sex", nil);
    editSexLabel.font = [UIFont systemFontOfSize:14.0f];
    [sexView addSubview:editSexLabel];
    
    x = 100;
    y = 0;
    width = 40;
    height = 50;
    manView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [sexView addSubview:manView];
    manView.userInteractionEnabled = YES;
    UITapGestureRecognizer * manTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(manClick)];
    [manView addGestureRecognizer:manTap];
    
    x = 100;
    y = 15;
    width = 20;
    height = 20;
    manBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [manBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [manBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [manBtn addTarget:self action:@selector(manClick) forControlEvents:UIControlEventTouchUpInside];
    manBtn.selected = YES;
    [sexView addSubview:manBtn];
    
    x = 140;
    height = 20;
    UILabel * manLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 40, height)];
    manLabel.text =  NSLocalizedString(@"man", nil);
    manLabel.font = [UIFont systemFontOfSize:14.0f];
    [sexView addSubview:manLabel];
    
    x = 180;
    y = 0;
    width = 40;
    height = 50;
    womanView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [sexView addSubview:womanView];
    womanView.userInteractionEnabled = YES;
    UITapGestureRecognizer * womanTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(womanClick)];
    [womanView addGestureRecognizer:womanTap];
    
    x += 10;
    y += 15;
    width = 20;
    height = 20;
    womanBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [womanBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [womanBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [womanBtn addTarget:self action:@selector(womanClick) forControlEvents:UIControlEventTouchUpInside];
    [sexView addSubview:womanBtn];
    
    x = 220;
    height = 20;
    UILabel * womanLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 60, height)];
    womanLabel.text =  NSLocalizedString(@"woman", nil);
    womanLabel.font = [UIFont systemFontOfSize:14.0f];
    [sexView addSubview:womanLabel];
    
    UITapGestureRecognizer * guestTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(resignClick)];
    [self.view addGestureRecognizer:guestTap];
}

#pragma mark ----------点击手机号
-(void)mobileClick
{
    [telePhoneTextField resignFirstResponder];
    mobileBtn.selected = !mobileBtn.selected;
    if (mobileBtn.selected) {
        emailBtn.selected = NO;
        mobileHeaderBtn.hidden = NO;
        emailHeaderLabel.hidden = YES;
        telePhoneTextField.keyboardType = UIKeyboardTypeNamePhonePad;
        telePhoneTextField.placeholder = NSLocalizedString(@"loginPromt", nil);
        addAddressTelePhoneBtn.hidden = NO;
    }
    else
    {
        emailBtn.selected = YES;
        emailHeaderLabel.hidden = NO;
        mobileHeaderBtn.hidden = YES;
        telePhoneTextField.keyboardType = UIKeyboardTypeDefault;
        telePhoneTextField.placeholder = NSLocalizedString(@"mineEmail", nil);
        addAddressTelePhoneBtn.hidden = YES;
    }
}


#pragma mark ----------点击邮箱
-(void)emailClick
{
    [telePhoneTextField resignFirstResponder];
    emailBtn.selected = !emailBtn.selected;
    if (emailBtn.selected) {
        mobileBtn.selected = NO;
        emailHeaderLabel.hidden = NO;
        mobileHeaderBtn.hidden = YES;
        telePhoneTextField.keyboardType = UIKeyboardTypeDefault;
        telePhoneTextField.placeholder = NSLocalizedString(@"mineEmail", nil);
        addAddressTelePhoneBtn.hidden = YES;
    }
    else
    {
        mobileBtn.selected = YES;
        mobileHeaderBtn.hidden = NO;
        emailHeaderLabel.hidden = YES;
        telePhoneTextField.keyboardType = UIKeyboardTypeNamePhonePad;
        telePhoneTextField.placeholder = NSLocalizedString(@"loginPromt", nil);
        addAddressTelePhoneBtn.hidden = NO;
    }
}


#pragma mark ---------- 改变地区或者国家
-(void)chooseArea
{
    AreaChooseController * acc = [[AreaChooseController alloc]init];
    [self.navigationController pushViewController:acc animated:YES];
    acc.areaCode = ^(NSString * areaCodeString,NSString * areaString)
    {
        areaCodeIdLabel.text = [NSString stringWithFormat:@"+%@",areaCodeString];
        areaLabel.text = areaString;
    };
}


#pragma mark -- 从通讯录添加职员
-(void)addTelePhone
{
    ABPeoplePickerNavigationController * peoplePicker = [[ABPeoplePickerNavigationController alloc]init];
    peoplePicker.peoplePickerDelegate = self;
    [self presentViewController:peoplePicker animated:NO completion:nil];
}

#pragma mark -- ABPeoplePickerNavigationControllerDelegate
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
    ABMutableMultiValueRef phoneMult = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString * fisrtName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString * lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    if (fisrtName == nil) {
        fisrtName = @"";
    }
    if (lastName == nil) {
        lastName = @"";
    }
    nameTextField.text = [NSString stringWithFormat:@"%@%@",lastName,fisrtName];
    
    NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < ABMultiValueGetCount(phoneMult); i ++) {
        NSString * aphone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMult, i);
        [phones addObject:aphone];
    }
    if (phones.count > 0) {
        telePhoneTextField.text = [phones objectAtIndex:0];
        telePhoneTextField.text = [telePhoneTextField.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -- ABPeoplePickerNavigationControllerDelegate
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}

//2_0到8_0系统使用的方法
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    ABMutableMultiValueRef phoneMult = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString * fisrtName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString * lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    if (fisrtName == nil) {
        fisrtName = @"";
    }
    if (lastName == nil) {
        lastName = @"";
    }
    nameTextField.text = [NSString stringWithFormat:@"%@%@",lastName,fisrtName];
    
    NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < ABMultiValueGetCount(phoneMult); i ++) {
        NSString * aphone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMult, i);
        [phones addObject:aphone];
    }
    if (phones.count > 0) {
        telePhoneTextField.text = [phones objectAtIndex:0];
        telePhoneTextField.text = [telePhoneTextField.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    return NO;
}



-(void)resignClick
{
    [nameTextField resignFirstResponder];
    [telePhoneTextField resignFirstResponder];
    [ceritificateTextField resignFirstResponder];
}

-(void)saveClick
{
    [self resignClick];
    if (areaCodeIdLabel.text == nil || [areaCodeIdLabel.text isEqualToString:@""] || [areaCodeIdLabel.text isKindOfClass:[NSNull class]]) {
        [PromptInfoView showMessage:@"errorAreaOrCountry"];
        return;
    }
    if (telePhoneTextField.text.length == 0) {
        [PromptInfoView showMessage:NSLocalizedString(@"errorAccount", nil) superView:self.view];
        return;
    }
    if (mobileBtn.selected) {
        if (![validateInputValue isPureNumandCharacters:telePhoneTextField.text]) {
            [PromptInfoView showMessage:NSLocalizedString(@"AccountError", nil) superView:self.view];
            return;
        }
    }
    if (emailBtn.selected) {
        if (![validateInputValue validateEmail:telePhoneTextField.text]) {
            [PromptInfoView showMessage:NSLocalizedString(@"AccountError", nil) superView:self.view];
            return;
        }
    }
    if (nameTextField.text.length < 1 || nameTextField.text.length > 20) {
        [PromptInfoView showMessage:NSLocalizedString(@"userNameLength", nil) superView:self.view];
        return;
    }
    
    NSString * locaString = NSLocalizedString(@"sureAdd", nil);
    NSString * telePhoneString = [NSString stringWithFormat:@"%@%@？",locaString,telePhoneTextField.text];
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:telePhoneString delegate:self cancelButtonTitle:NSLocalizedString(@"alterCancel", nil) otherButtonTitles:NSLocalizedString(@"alterSure", nil), nil];
    [alert show];
}


#pragma mark -- alertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self saveGuestInfo];
    }
}


-(void)saveGuestInfo
{
    [self.view endEditing:YES];
    if ([self IsHaveNetwork]) {
        return;
    }
    NSString * postString = nil;
    [self startWaiting:nil color:nil addToView:self.view];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    saveDict = [[NSMutableDictionary alloc]init];
    if (_groupId) {
        [saveDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
        [saveDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
        [saveDict setValue:_communityId forKey:@"communityId"];
        [saveDict setValue:_groupId forKey:@"groupId"];
        if([validateInputValue isPureNumandCharacters:telePhoneTextField.text]==YES)
        {
            [saveDict setValue:telePhoneTextField.text forKey:@"account"];
            [saveDict setValue:[areaCodeIdLabel.text substringFromIndex:1] forKey:@"areaCode"];
        }
        else if ([validateInputValue validateEmail:telePhoneTextField.text])
        {
            [saveDict setValue:telePhoneTextField.text forKey:@"account"];
        }
        [saveDict setValue:nameTextField.text forKey:@"name"];
        [saveDict setValue:ceritificateTextField.text forKey:@"identity"];
        [saveDict setValue:sexFlag forKey:@"gender"];
        postString = ADDGROUPCLIENT;
        if([self getTimestamp:_groupId timeKey:@"groupClient_timestamp"] != nil)
        {
            [saveDict setValue:[self getTimestamp:_groupId timeKey:@"groupClient_timestamp"] forKey:@"timestamp"];
        }
        else
        {
            [saveDict setValue:@"" forKey:@"timestamp"];
        }

    }
    else
    {
        [saveDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
        [saveDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
        [saveDict setValue:nameTextField.text forKey:@"name"];
        [saveDict setValue:sexFlag forKey:@"gender"];
        [saveDict setValue:ceritificateTextField.text forKey:@"identify"];
        if([validateInputValue isPureNumandCharacters:telePhoneTextField.text]==YES)
        {
            [saveDict setValue:telePhoneTextField.text forKey:@"mobile"];
            [saveDict setValue:[areaCodeIdLabel.text substringFromIndex:1] forKey:@"areaCode"];
        }
        else if ([validateInputValue validateEmail:telePhoneTextField.text])
        {
            [saveDict setValue:telePhoneTextField.text forKey:@"email"];
        }
        postString = ADDGUEST;
        if([self getTimestamp:_communityId timeKey:@"customer_timestamp"] != nil)
        {
            [saveDict setValue:[self getTimestamp:_communityId timeKey:@"customer_timestamp"] forKey:@"timestamp"];
        }
        else
        {
            [saveDict setValue:@"" forKey:@"timestamp"];
        }
        [saveDict setValue:_communityId forKey:@"communityId"];
    }
    
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    [rnk initWithPath:POST(postString) params:saveDict block:^(id downLoadData, id error)
     {
         if (_groupId) {
             if ([downLoadData objectForKey:@"cache"] ) {
                 if ([[[downLoadData objectForKey:@"cache"] objectForKey:@"status"]integerValue] == 0)
                 {
                      [self updateTimestamp:_groupId timeKey:@"groupClient_timestamp" timeKeyValue:[[downLoadData objectForKey:@"cache"] objectForKey:@"timestamp"]];
                 }
                 GuestModel * model = [[GuestModel alloc]init];
                 NSMutableDictionary *data = [[NSMutableDictionary alloc]init];
                 data = [downLoadData objectForKey:@"data"];
                 model.email = [data objectForKey:@"email"];
                 model.firstChar = [data objectForKey:@"firstchar"];
                 model.sex = [data objectForKey:@"gender"];
                 model.headUrl = [data objectForKey:@"headurl"];
                 model.guestid = [data objectForKey:@"id"];
                 model.certificaties = [data objectForKey:@"identity"];
                 model.telePhone = [data objectForKey:@"mobile"];
                 model.guestName= [data objectForKey:@"name"];
                 model.isGroupManager = @"0";
                 if([validateInputValue isPureNumandCharacters:telePhoneTextField.text]==YES)
                 {
                     model.areaCode = [areaCodeIdLabel.text substringFromIndex:1];
                 }
                 NSMutableArray * guestArray = [[NSMutableArray alloc]init];
                 [guestArray addObject:model];
                 
                 GroupGuestLogic *groupGuest = [[GroupGuestLogic alloc]init];
                 [groupGuest insertGroupGuestInfo:model groupName:_groupName groupStartTime:nil groupEndTime:nil groupId:_groupId];
                 GuestManagerViewController * gmvc = [[GuestManagerViewController alloc]init];
                 gmvc.communityid = _communityId;
                 gmvc.communityRole = _communityRole;
                 //            gmvc.allocateRoomPower = _allocateRoomPower;
                 gmvc.model = model;
                 gmvc.groupId = _groupId;
                 editImgView.image = [UIImage imageNamed:@"ic_save"];
                 [self.navigationController pushViewController:gmvc animated:YES];
             }
         }
         else
         {
             if ([downLoadData objectForKey:@"cache"] != nil) {
                 if([@"1" isEqualToString:[NSString stringWithFormat:@"%@",[[downLoadData objectForKey:@"cache"] objectForKey:@"status"]]]==YES)
                 {
                     if ([downLoadData objectForKey:@"data"] != nil)
                     {
                         NSDictionary * guestDict = [downLoadData objectForKey:@"data"];
                         GuestModel * model = [[GuestModel alloc]init];
                         model.guestid = [guestDict objectForKey:@"id"];
                         model.guestName = [guestDict objectForKey:@"name"];
                         model.sex = [guestDict objectForKey:@"gender"];
                         model.headUrl = [guestDict objectForKey:@"headurl"];
                         model.telePhone = [guestDict objectForKey:@"mobile"];
                         model.email = [guestDict objectForKey:@"email"];
                         model.certificaties = [guestDict objectForKey:@"identityId"];
                         if([validateInputValue isPureNumandCharacters:telePhoneTextField.text]==YES)
                         {
                             model.areaCode = [areaCodeIdLabel.text substringFromIndex:1];
                         }
                         GuestManagerViewController * gmvc = [[GuestManagerViewController alloc]init];
                         gmvc.communityid = _communityId;
                         gmvc.communityRole = _communityRole;
                         //            gmvc.allocateRoomPower = _allocateRoomPower;
                         gmvc.model = model;
                         gmvc.groupId = _groupId;
                         GuestLogic *guestLogic = [[GuestLogic alloc]init];
                         [guestLogic insertStaffData:model withCommuntiyId:_communityId];
                         
                         editImgView.image = [UIImage imageNamed:@"ic_save"];
                         [self.navigationController pushViewController:gmvc animated:YES];
                     }
                 }
                 else
                 {
                     [self updateTimestamp:_communityId timeKey:@"customer_timestamp" timeKeyValue:[[downLoadData objectForKey:@"cache"] objectForKey:@"timestamp"]];
                     if ([downLoadData objectForKey:@"data"] != nil)
                     {
                         NSDictionary * guestDict = [downLoadData objectForKey:@"data"];
                         GuestModel * model = [[GuestModel alloc]init];
                         model.guestid = [guestDict objectForKey:@"id"];
                         model.guestName = [guestDict objectForKey:@"name"];
                         model.sex = [guestDict objectForKey:@"gender"];
                         model.headUrl = [guestDict objectForKey:@"headurl"];
                         model.telePhone = [guestDict objectForKey:@"mobile"];
                         model.email = [guestDict objectForKey:@"email"];
                         model.certificaties = [guestDict objectForKey:@"identityId"];
                         model.firstChar=[guestDict objectForKey:@"firstChar"];
                         model.firstChar = [self firstChar:model.guestName];
                         if([validateInputValue isPureNumandCharacters:telePhoneTextField.text]==YES)
                         {
                             model.areaCode = [areaCodeIdLabel.text substringFromIndex:1];
                         }
                         GuestManagerViewController * gmvc = [[GuestManagerViewController alloc]init];
                         gmvc.communityid = _communityId;
                         gmvc.communityRole = _communityRole;
                         //            gmvc.allocateRoomPower = _allocateRoomPower;
                         gmvc.model = model;
                         gmvc.groupId = _groupId;
                         GuestLogic *guestLogic = [[GuestLogic alloc]init];
                         [guestLogic insertStaffData:model withCommuntiyId:_communityId];
                         
                         editImgView.image = [UIImage imageNamed:@"ic_save"];
                         [self.navigationController pushViewController:gmvc animated:YES];
                     }
                 }
             }

         }
        if (error != nil) {
             [PromptInfoView showMessage:[error objectForKey:@"info"] superView:self.view];
         }
         [self stopWaiting];
     }
     ];
}


-(void)womanClick
{
    if (!womanBtn.enabled) {
        return;
    }
    if (womanBtn.selected) {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";
        
    }
    else
    {
        manBtn.selected = NO;
        womanBtn.selected = YES;
        sexFlag = @"0";
    }
}

-(void)manClick
{
    if (!manBtn.enabled) {
        return;
    }
    if (manBtn.selected) {
        manBtn.selected = NO;
        womanBtn.selected = YES;
        sexFlag = @"0";
    }
    else
    {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";
    }
}



-(void)hiddenKeyboard
{
    [self resignClick];
}




@end
